#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/6/22 14:43
from domain.report.common.period_enum import PeriodEnum
from domain.report.common.str_util import del_html_element
from domain.report.dto.basic_info_dto import BasicInfoDTO, BasicInfoSchema
from typing import List


_value_index_name = ["company_name", "chairman", "reg_address", "office_postCode", "website", "secretary_name",
                     "secretary_phone", "secretary_mail", "holder_num", "bonusSharesNum_per10", "dividend_per10",
                     "transfersNum_per10"]


def xbrl2dtos(data, stock_code, period:PeriodEnum) -> List[BasicInfoDTO]:
    columns = data["columns"]
    rows = data["rows"]
    dto_list = list()
    for i in range(len(columns)):
        dto = BasicInfoDTO()
        dto.stock_code:str = stock_code
        dto.stock_id = int(stock_code.split(".")[0])
        dto.period = period
        dto.year = int(del_html_element(data["columns"][i]["title"]))
        for j in range(len(data["rows"])):
            val = del_html_element(rows[j][del_html_element(columns[i]["field"])])
            val = val.replace("-", "") if val and "-" in val else val
            BasicInfoSchema.set(dto, _value_index_name[j], val)
        dto_list.append(dto)
    return dto_list
